package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    @Autowired
    private JedisPool jedisPool;

    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile){
        log.info("[套餐-上传图片]fileName:{},size:{}",multipartFile.getOriginalFilename(),multipartFile.getSize());
        //获取原始文件名
        String originalFilename = multipartFile.getOriginalFilename();
        //使用UUID构建不重复的文件名
        String fileName= UUID.randomUUID().toString().replace("-","")+"_"+originalFilename;
        //获取输入流上传
        try(InputStream is = multipartFile.getInputStream()){
            //上传到七牛云
            QiniuUtils.upload2Qiniu(is,fileName);
        }catch (RuntimeException|IOException e) {
            log.error("",e);
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }
        try(Jedis jedis=jedisPool.getResource()) {
            jedis.sadd(RedisConst.SETMEAL_PIC_RESOURCES,fileName);
        }
        //返回前段图片url地址
        String pic=QiniuUtils.QINIU_IMG_URL_PRE+fileName;
        return new Result(true,MessageConst.PIC_UPLOAD_SUCCESS,pic);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds ){
        log.info("[套餐-新增]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        //处理图片前缀
        if (!StringUtils.isEmpty(setmeal.getImg())){
            String img=setmeal.getImg().replace(QiniuUtils.QINIU_IMG_URL_PRE,"");
            setmeal.setImg(img);
        }
        //rpc操作数据
        try {
            setMealService.add(setmeal,checkgroupIds);
            return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
        } catch (RuntimeException e) {
            log.error("",e);
            return new Result(false,MessageConst.ADD_SETMEAL_FAIL+":"+e.getMessage());
        }
    }

    @GetMapping("/findPage")
    public PageResult findPage(QueryPageBean queryPageBean){
        log.info("[套餐-分页]data:{}",queryPageBean);
        try {
            return setMealService.findPage(queryPageBean);
        } catch (RuntimeException e) {
            log.error("",e);
            return new PageResult(0L, Collections.emptyList());
        }
    }
}
